﻿using System;
using ApplicationLibrary.DBManager;
using ApplicationLibrary.SqlConnector;

#region RegisterTable

namespace ApplicationLibrary.ModuleData
{
    public class RegisterModule : ApplicationLibrary.SqlConnector.RegisterModule
    {
        override public int CurrentVersion()
        {
            return 2;
        }

        override public string Module()
        {
            return "APPL";
        }

        override public string Application()
        {
            return "APPM";
        }

        public override Version DllVersion
        {
            get
            {
                return System.Reflection.Assembly.GetExecutingAssembly().GetName().Version;
            }
        }

        public override void AddScrounger(iScrounger frm, NameSpace nameSpace)
        {
            throw new NotImplementedException();
        }

        override protected bool CreateDBTables()
        {
            SqlCreateTable newTable = new SqlCreateTable(SqlABConnection);

            #region Users Table

            if (SearchTable(AM_Users.Name))
            {
                newTable.NewTable(AM_Users.Name);
                newTable.AddColumn(AM_Users.Username);
                newTable.AddColumn(AM_Users.Password);
                newTable.AddColumn(AM_Users.Surname);
                newTable.AddColumn(AM_Users.UserType);
                newTable.AddColumn(AM_Users.Expired);
                newTable.AddColumn(AM_Users.ExpireDate);
                newTable.AddColumn(AM_Users.ChangePassword);
                newTable.AddColumn(AM_Users.Blocked);
                newTable.AddPrimaryKey(AM_Users.Username);
                newTable.Create();
            }

            #endregion

            #region Lock Table

            if (SearchTable(AM_Locks.Name))
            {
                newTable.NewTable(AM_Locks.Name);
                newTable.AddColumn(AM_Locks.ComputerName);
                newTable.AddColumn(AM_Locks.Username);
                newTable.AddColumn(AM_Locks.TableName);
                newTable.AddColumn(AM_Locks.LockKey);
                newTable.AddColumn(AM_Locks.LockDate);

                newTable.AddPrimaryKey(AM_Locks.TableName, AM_Locks.LockKey);
                newTable.Create();
            }

            #endregion

            #region DBVersion

            if (SearchTable(AM_Version.Name))
            {
                newTable.NewTable(AM_Version.Name);
                newTable.AddColumn(AM_Version.Application);
                newTable.AddColumn(AM_Version.Module);
                newTable.AddColumn(AM_Version.Version);

                newTable.AddPrimaryKey(new iColumn[] { AM_Version.Application, AM_Version.Module });
                newTable.Create();
            }

            #endregion

            #region AM_Descriptions

            if (SearchTable(AM_Descriptions.Name))
            {
                newTable.NewTable(AM_Descriptions.Name);

                newTable.AddColumn(AM_Descriptions.ID);
                newTable.AddColumn(AM_Descriptions.Category);
                newTable.AddColumn(AM_Descriptions.Description);

                newTable.AddPrimaryKey(new iColumn[] { AM_Descriptions.ID });

                newTable.Create();
            }

            #endregion

            #region AM_Counter

            //newTable.DropTable(AM_Counter.Name);

            if (SearchTable(AM_Counter.Name))
            {
                newTable.NewTable(AM_Counter.Name);

                newTable.AddColumn(AM_Counter.Year);
                newTable.AddColumn(AM_Counter.Type);
                newTable.AddColumn(AM_Counter.Description);
                newTable.AddColumn(AM_Counter.HasPrefix);
                newTable.AddColumn(AM_Counter.PrefixRO);
                newTable.AddColumn(AM_Counter.PrefixLen);
                newTable.AddColumn(AM_Counter.PrefixValue);
                newTable.AddColumn(AM_Counter.PrefixMask);
                newTable.AddColumn(AM_Counter.PrefixType);
                newTable.AddColumn(AM_Counter.PrefixSep);
                newTable.AddColumn(AM_Counter.HasSuffix);
                newTable.AddColumn(AM_Counter.SuffixRO);
                newTable.AddColumn(AM_Counter.SuffixLen);
                newTable.AddColumn(AM_Counter.SuffixValue);
                newTable.AddColumn(AM_Counter.SuffixMask);
                newTable.AddColumn(AM_Counter.SuffixType);
                newTable.AddColumn(AM_Counter.SuffixSep);
                newTable.AddColumn(AM_Counter.InvertSufPref);
                newTable.AddColumn(AM_Counter.CodeType);
                newTable.AddColumn(AM_Counter.CodeLen);
                newTable.AddColumn(AM_Counter.CodeKey);

                newTable.AddPrimaryKey(new iColumn[] { AM_Counter.Year, AM_Counter.Type });

                newTable.Create();
            }

            #endregion

            #region AM_CounterValue

            if (SearchTable(AM_CounterValue.Name))
            {
                newTable.NewTable(AM_CounterValue.Name);

                newTable.AddColumn(AM_CounterValue.Type);
                newTable.AddColumn(AM_CounterValue.Code);
                newTable.AddColumn(AM_CounterValue.Description);
                newTable.AddColumn(AM_CounterValue.TextValue);
                newTable.AddColumn(AM_CounterValue.NumericValue);

                newTable.AddPrimaryKey(new iColumn[] { AM_CounterValue.Type, AM_CounterValue.Code });

                newTable.Create();
            }

            #endregion

            #region AM_Codes

            if (SearchTable(AM_Codes.Name))
            {
                newTable.NewTable(AM_Codes.Name);

                newTable.AddColumn(AM_Codes.CodeType);
                newTable.AddColumn(AM_Codes.Description);

                newTable.AddPrimaryKey(new iColumn[] { AM_Codes.CodeType });

                newTable.Create();
            }

            #endregion

            #region AM_CodeSegment

            if (SearchTable(AM_CodeSegment.Name))
            {
                newTable.NewTable(AM_CodeSegment.Name);

                newTable.AddColumn(AM_CodeSegment.CodeType);
                newTable.AddColumn(AM_CodeSegment.Segment);
                newTable.AddColumn(AM_CodeSegment.Description);
                newTable.AddColumn(AM_CodeSegment.InputType);
                newTable.AddColumn(AM_CodeSegment.InputLen);

                newTable.AddPrimaryKey(new iColumn[] { AM_CodeSegment.CodeType, AM_CodeSegment.Segment });

                newTable.Create();
            }

            #endregion

            #region AM_Preferences

            if (SearchTable(AM_Preferences.Name))
            {
                newTable.NewTable(AM_Preferences.Name);

                newTable.AddColumn(AM_Preferences.PrefType);
                newTable.AddColumn(AM_Preferences.Computer);
                newTable.AddColumn(AM_Preferences.Username);
                newTable.AddColumn(AM_Preferences.Application);
                newTable.AddColumn(AM_Preferences.Preferences);

                newTable.AddPrimaryKey(new iColumn[] {   AM_Preferences.PrefType, AM_Preferences.Computer,
                                                    AM_Preferences.Username, AM_Preferences.Application});

                newTable.Create();
            }

            #endregion

            return true;
        }

        override protected bool UpdateDBTables()
        {
            if (dbVersion < 2)
            {
                SqlCreateTable alterMaster = new SqlCreateTable(SqlABConnection);

                if (!SearchColumn(AM_Counter.InvertSufPref))
                {
                    alterMaster.AlterTable(AM_Counter.Name);
                    alterMaster.AddColumn(AM_Counter.InvertSufPref);
                    alterMaster.Alter();
                }
            }
            return true;
        }
    }
}

#endregion